package com.lst.projectlib.component.datetimepicker;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;

import com.lst.projectlib.R;
import com.lst.projectlib.util.DensityUtil;

public class DoubleTimePickerActivity extends Activity implements
		OnClickListener, OnTimeChangedListener {

	public static final String INTENT_BUNDLE_KEY_INITIAL_HOUR_LEFT = "initial_hour_left";
	public static final String INTENT_BUNDLE_KEY_INITIAL_MINUTE_LEFT = "initial_minute_left";
	public static final String INTENT_BUNDLE_KEY_INITIAL_HOUR_RIGHT = "initial_hour_right";
	public static final String INTENT_BUNDLE_KEY_INITIAL_MINUTE_RIGHT = "initial_minute_right";
	public static final String INTENT_BUNDLE_KEY_INITIAL_TITLE = "initial_title";
	public static final String INTENT_BUNDLE_KEY_SELECT_DOUBLE_TIME = "select_double_time";
	public static final String INTENT_BUNDLE_KEY_IS_NOT_CHECK_EFFECTIVE = "is_not_check_effective";
	private TimePicker mTimePickerLeft;
	private TimePicker mTimePickerRight;
	boolean mIs24HourView = true;
	int request_code = 0;
	private boolean isNotCheckEffective = false;
	
	private LinearLayout ll_title_container = null;
	private LinearLayout ll_main = null;
	private TextView tv_title_left = null;
	private TextView tv_title_right = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_double_time_picker);

		TextView tv_ok = (TextView) this.findViewById(R.id.tv_ok);
		TextView tv_cancel = (TextView) this.findViewById(R.id.tv_cancel);
		tv_ok.setOnClickListener(this);
		tv_cancel.setOnClickListener(this);

		mTimePickerLeft = (TimePicker) this.findViewById(R.id.time_picker_left);
		mTimePickerRight = (TimePicker) this.findViewById(R.id.time_picker_right);

		// initialize state
		mTimePickerLeft.setIs24HourView(mIs24HourView);
		mTimePickerLeft.setOnTimeChangedListener(this);
		
		mTimePickerRight.setIs24HourView(mIs24HourView);
		mTimePickerRight.setOnTimeChangedListener(this);
		
		ll_title_container = (LinearLayout) this.findViewById(R.id.ll_title_container);
		ll_main = (LinearLayout) this.findViewById(R.id.ll_main);
		tv_title_left = (TextView) this.findViewById(R.id.tv_title_left);
		tv_title_right = (TextView) this.findViewById(R.id.tv_title_right);
		
		initData();
	}

	private void initData() {
		Intent intent = getIntent();

		if (intent != null) {
			int leftHour = intent.getIntExtra(INTENT_BUNDLE_KEY_INITIAL_HOUR_LEFT, 0);
			int leftMinute = intent.getIntExtra(INTENT_BUNDLE_KEY_INITIAL_MINUTE_LEFT, 0);
			int rightHour = intent.getIntExtra(INTENT_BUNDLE_KEY_INITIAL_HOUR_RIGHT, 0);
			int rightMinute = intent.getIntExtra(INTENT_BUNDLE_KEY_INITIAL_MINUTE_RIGHT, 0);
			mTimePickerLeft.setCurrentHour(leftHour);
			mTimePickerLeft.setCurrentMinute(leftMinute);
			mTimePickerRight.setCurrentHour(rightHour);
			mTimePickerRight.setCurrentMinute(rightMinute);
			request_code = intent.getIntExtra("request_code", 0);
			
			String[] title = intent.getStringArrayExtra(INTENT_BUNDLE_KEY_INITIAL_TITLE);
			if (title != null && title.length == 2) {
				tv_title_left.setText(title[0]);
				tv_title_right.setText(title[1]);
				ll_main.setPadding(0, DensityUtil.dip2px(this, 12), 0, 0);
				ll_title_container.setVisibility(View.VISIBLE);
			} else {
				ll_main.setPadding(0, 0, 0, 0);
				ll_title_container.setVisibility(View.GONE);
			}
			
			isNotCheckEffective = intent.getBooleanExtra(INTENT_BUNDLE_KEY_IS_NOT_CHECK_EFFECTIVE, false);
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.tv_cancel) {
			finish();
		} else if (v.getId() == R.id.tv_ok) {
			if (!isNotCheckEffective) {
				int leftHour = mTimePickerLeft.getCurrentHour();
				int leftMinute = mTimePickerLeft.getCurrentMinute();
				int rightHour = mTimePickerRight.getCurrentHour();
				int rightMinute = mTimePickerRight.getCurrentMinute();
				if (leftHour > rightHour) {
					Toast.makeText(this, "起始时间不能大于终止时间", Toast.LENGTH_SHORT).show();
					return;
				}
				
				if (leftHour == rightHour && leftMinute > rightMinute) {
					Toast.makeText(this, "起始时间不能大于终止时间", Toast.LENGTH_SHORT).show();
					return;
				}
				
				if (leftHour == rightHour && leftMinute == rightMinute) {
					Toast.makeText(this, "起始时间不能等于终止时间", Toast.LENGTH_SHORT).show();
					return;
				}
			}
			
			tryNotifyTimeSet();
			finish();
		}
	}

	public void updateTime(int leftHour, int leftMinute, int rightHour, int rightMinute) {
		mTimePickerLeft.setCurrentHour(leftHour);
		mTimePickerLeft.setCurrentMinute(leftMinute);
		mTimePickerRight.setCurrentHour(rightHour);
		mTimePickerRight.setCurrentMinute(rightMinute);
	}

	private void tryNotifyTimeSet() {
		if (mTimePickerLeft != null && mTimePickerRight != null) {
			mTimePickerLeft.clearFocus();
			mTimePickerRight.clearFocus();
			
			Intent intent = new Intent();
			intent.putExtra("request_code", request_code);
//			intent.putExtra("select_hour_left", mTimePickerLeft.getCurrentHour());
//			intent.putExtra("select_minute_left", mTimePickerLeft.getCurrentMinute());
//			intent.putExtra("select_hour_right", mTimePickerRight.getCurrentHour());
//			intent.putExtra("select_minute_right", mTimePickerRight.getCurrentMinute());
			
			String leftHour = mTimePickerLeft.getCurrentHour() < 10 ? ("0" + mTimePickerLeft.getCurrentHour()) : String.valueOf(mTimePickerLeft.getCurrentHour());
			String leftMinute = mTimePickerLeft.getCurrentMinute() < 10 ? ("0" + mTimePickerLeft.getCurrentMinute()) : String.valueOf(mTimePickerLeft.getCurrentMinute());
			String rightHour = mTimePickerRight.getCurrentHour() < 10 ? ("0" + mTimePickerRight.getCurrentHour()) : String.valueOf(mTimePickerRight.getCurrentHour());
			String rightMinute = mTimePickerRight.getCurrentMinute() < 10 ? ("0" + mTimePickerRight.getCurrentMinute()) : String.valueOf(mTimePickerRight.getCurrentMinute());
			
			intent.putExtra(INTENT_BUNDLE_KEY_SELECT_DOUBLE_TIME, leftHour + ":" + leftMinute + "-" + rightHour + ":" + rightMinute);
			this.setResult(RESULT_OK, intent);
		}
	}

	@Override
	public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

	}
}
